<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-input v-model="form.id" type="hidden"></el-input>
				<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
					<el-form-item label="上级机构" prop="parentId">
						<el-cascader v-model="form.parentId" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
					<el-form-item label="机构名称" prop="fullName">
						<el-input v-model="form.fullName" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="机构简称" prop="shortName">
						<el-input v-model="form.shortName" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="机构代码" prop="code">
						<el-input v-model="form.code" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="所属城市" prop="area">
						<el-cascader v-model="form.area" :options="citys" change-on-select filterable @change="handleAreaChange" clearable style="width: 100%;"></el-cascader>
						<el-input v-model="form.provinceId" type="hidden"></el-input>
						<el-input v-model="form.cityId" type="hidden"></el-input>
						<el-input v-model="form.countyId" type="hidden"></el-input>
					</el-form-item>
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="form.sort" controls-position="right" :min="1" ></el-input-number>
					</el-form-item>
					<el-form-item label="是否有效" prop="status">
						<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
					</el-form-item>
					<el-form-item label="备注" prop="description">
						<el-input v-model="form.description" clearable type="textarea"></el-input>
					</el-form-item>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	import { regionData } from "element-china-area-data";

	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					parentId: "",
					fullName: "",
					sort: 1,
					status: "1",
					description: ""
				},
				//验证规则
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					fullName: [
						{required: true, message: '请输入机构全称'}
					],
					shortName: [
						{required: true, message: '请输入机构简称'}
					],
					code: [
						{required: true, message: '请输入机构代码'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "fullName",
					emitPath: false,
					checkStrictly: true
				},
				citys: regionData
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup(){
				var res = await this.$API.system.company.list.get();
				this.groups = res.data;
			},
			handleAreaChange() {
				this.form.provinceId = this.form.area[0];
				if( this.form.area.length > 1) {
					this.form.cityId = this.form.area[1];
				}
				if( this.form.area.length > 2) {
					this.form.countyId = this.form.area[2];
				}
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.company.save.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.parentId = data.parentId
				this.form.fullName = data.fullName
				this.form.shortName = data.shortName
				this.form.code = data.code
				this.form.provinceId = data.provinceId
				this.form.cityId = data.cityId
				this.form.countyId = data.countyId
                var area = []
  				if(data.provinceId!=null) {
					area.push(data.provinceId);
                }
                if(data.cityId!=null) {
					area.push(data.cityId);
                }
                if(data.countyId!=null) {
					area.push(data.countyId);
                }
                this.form.area = area
				this.form.status = data.status
				this.form.sort = data.sort
				this.form.description = data.description

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
